package com.nxy.cpfsfe.service.factory;

import com.adtec.starring.exception.BaseException;
import com.adtec.starring.global.SysDef;
import com.adtec.starring.respool.SystemParameter;
import com.nxy.cpfsfe.enumtype.MsgCdOrTpEnum;
import com.nxy.cpfsfe.enumtype.RespCd;
import com.nxy.cpfsfe.util.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * 信用卡分期支付查询
 * Created by wyj on 2019/12/27.
 */
public class CrCrdInstalPQryServer implements ForwardServer{
    private static CrCrdInstalPQryServer server = new CrCrdInstalPQryServer();
    private CrCrdInstalPQryServer(){}
    public static CrCrdInstalPQryServer getInstance(){
        return server;
    }
    private final static Logger LOGGER = LoggerFactory.getLogger(CrCrdInstalPQryServer.class);

    @Override
    public String buildEsbXml(Map<String, String> map) {
        LOGGER.info("---------信用卡分期支付查询报文CBS0010810--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CBS" + SysDef.FILE_SP + "CBS_0010810.xml");
            //报文头
            XmlCBSBuildUtil.buildXmlRequestHead(xmlReqDoc, MsgCdOrTpEnum.CBS_0010810.getCode(), map);
            //报文体
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/cardNb ",StringUtil.trimObject(map.get("/root/MsgBody/PyerInf/PyerAcctNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/transAmt ",StringUtil.trimObject(map.get("/root/MsgBody/TrxInf/TrxAmt")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/MerCode ",StringUtil.trimObject(map.get("/root/MsgBody/MrchntInf/MrchntNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/MerName ",StringUtil.trimObject(map.get("/root/MsgBody/MrchntInf/MrchntNm")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/goodsId ",StringUtil.trimObject(map.get("/root/MsgBody/GoodsInf/GoodsNo")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/goodsType ",StringUtil.trimObject(map.get("/root/MsgBody/GoodsInf/GoodsTp")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/goodsName ",StringUtil.trimObject(map.get("/root/MsgBody/GoodsInf/GoodsNm")));
            xmlReqDoc.setNodeContentTrim("/transaction/body/request/memo ",StringUtil.trimObject(map.get("/root/MsgBody/TrxInf/TrxRemark")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("信用卡分期支付查询报文CBS0010810失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "信用卡分期支付查询报文CBS0010810失败");
        }
    }

    @Override
    public String handleRpsMsg(Map<String, String> map, String respMsg) {
        LOGGER.info("---------信用卡分期支付查询回执报文CBS0010811--------");
        try {
            XMLNodeUtil xmlReqDoc = new XMLNodeUtil(XMLNodeUtil.FROM_FILE,
                    SysDef.WORK_DIR + SystemParameter.SRC_PATH + SysDef.FILE_SP + "xml" + SysDef.FILE_SP + "CPFS" + SysDef.FILE_SP + "CPFS_311.xml");
            //报文头
            XmlCPFSBuildUtil.buildXmlResponseHead(xmlReqDoc, MsgCdOrTpEnum.MSGTD_311.getCode(), map);
            //解析cbs0010811报文
            if(StringUtils.isNotBlank(respMsg)){
                Map<String,String> rpsmap = XmlUtil.xml2Map(respMsg);
                String  retCd = StringUtil.trimObject(rpsmap.get("/transaction/header/status/retCd"));
                //返回码前两位为00代表成功
                if(retCd.startsWith("00")&&retCd.length() ==6){
                    map.put("resultCode",StringUtil.trimObject(rpsmap.get("/transaction/body/response/retCode")));
                    map.put("msg",StringUtil.trimObject(rpsmap.get("/transaction/body/response/retMsg")));
                    map.put("teamFlag",StringUtil.trimObject(rpsmap.get("/transaction/body/response/teamFlag")));
                    map.put("teamMsg",StringUtil.trimObject(rpsmap.get("/transaction/body/response/teamMsg")));
                    map.put("instalment",StringUtil.trimObject(rpsmap.get("/transaction/body/response/instalment")));
                    map.put("transDesc",StringUtil.trimObject(rpsmap.get("/transaction/body/response/transDesc")));
                }else{
                    map.put("msg", StringUtil.trimObject(rpsmap.get("/transaction/header/status/desc")));
                    map.put("resultCode",retCd);
                }
            }else{
                LOGGER.error("cbs0010811回执报文为空，请联系");
                map.put("msg", "cbs0010811回执报文为空，请联系");
                map.put("resultCode",RespCd.NCPO0005);
            }
            //报文体
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/TrxStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsCd",  StringUtil.trimObject(map.get("resultCode")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/BizInf/BizStsDesc",  StringUtil.trimObject(map.get("msg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/TeamFlg",  StringUtil.trimObject(map.get("teamFlag")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/TeamMsg",  StringUtil.trimObject(map.get("teamMsg")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/AcctInf/Instalment",  StringUtil.trimObject(map.get("instalment")));
            xmlReqDoc.setNodeContentTrim("/root/MsgBody/TransDesc",  StringUtil.trimObject(map.get("transDesc")));
            return xmlReqDoc.toString();
        } catch (Exception e) {
            LOGGER.error("信用卡分期支付查询回执报文CBS0010811失败", e);
            throw new BaseException(RespCd.NCPS2402, e, "信用卡分期支付查询回执报文CBS0010811失败");
        }
    }
}